package com.yd.location.module.cms.controller;

import com.yd.location.common.annotation.DeleteUrl;
import com.yd.location.common.annotation.ListUrl;
import com.yd.location.common.annotation.SaveUrl;
import com.yd.location.common.annotation.UpdateUrl;
import com.yd.location.common.cache.RedisKeyEnum;
import com.yd.location.common.cache.RedisUtil;
import com.yd.location.common.spring.mpbase.MyController;
import com.yd.location.common.spring.mpbase.MyEntity;
import com.yd.location.common.spring.mpbase.MyService;
import com.yd.location.common.spring.pagination.PageRequestDTO;
import com.yd.location.common.web.ParamUtil;
import com.yd.location.common.web.Resp;
import com.yd.location.module.cms.domain.Advert;
import com.yd.location.module.cms.mapper.AdvertMapper;
import com.yd.location.module.cms.service.AdvertService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Created by zk on 2020-07-14 20:54
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/cms/advert")
public class AdvertController extends MyController<AdvertMapper, Advert, PageRequestDTO> {
    @Resource
    private AdvertService advertService;
    @Resource
    private RedisTemplate redisTemplate;
    private static final String ADVERT_KEY = "advert";

    @Override
    public MyService<AdvertMapper, Advert> getService() {
        return advertService;
    }

    @GetMapping("/app_list")
    public Resp appList() {
        List<Advert> adverts = (List<Advert>) redisTemplate.opsForValue().get(ADVERT_KEY);
        if (ParamUtil.isBlank(adverts)) {
            adverts = advertService.lambdaQuery()
                    .orderByAsc(Advert::getSortValue)
                    .orderByDesc(MyEntity::getCreateTime)
                    .list();
            redisTemplate.opsForValue().set(ADVERT_KEY,adverts);
        }
        return Resp.success(adverts);
    }

    @ListUrl
    @Override
    public Resp list(PageRequestDTO condition) {
        return super.list(condition);
    }

    @SaveUrl
    @Override
    public Resp save(Advert entity) {
        super.save(entity);
        redisTemplate.delete(ADVERT_KEY);
        return Resp.success("添加成功");
    }

    @UpdateUrl
    @Override
    public Resp update(Advert entity) {
        super.update(entity);
        redisTemplate.delete(ADVERT_KEY);
        return Resp.success("修改成功");
    }

    @DeleteUrl
    @Override
    public Resp delete(String[] ids) {
        super.delete(ids);
        redisTemplate.delete(ADVERT_KEY);
        return Resp.success("删除成功");
    }
}
